/*******************************************************************************
*  file    : devicemodel.hpp
*  created : 20.08.2013
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#ifndef DEVICEMODEL_HPP
#define DEVICEMODEL_HPP

#include <QAbstractItemModel>
#include <QColor>
#include <archivedb.hpp>
#include <utils.hpp>
#include <QMap>

#define DM_IDEX_DATA_ROLE (Qt::UserRole+1)
#define DM_LAST_INFO_ROLE (Qt::UserRole+2)
#define DM_LAST_POS_ROLE  (Qt::UserRole+3)
#define DM_LAST_SMS_DATE  (Qt::UserRole+4)
#define DM_WORKING_TASKS  (Qt::UserRole+5)

#define DM_COLUMN_COUNT      11
#define DM_NAME_IDX          0
#define DM_PHONE_NUMBER_IDX  1
#define DM_STATUS_IDX        2
#define DM_BAT_CHANGE_IDX    3
#define DM_MEMO_IDX          4
#define DM_LAST_INFO_IDX     5
#define DM_LAST_POS_IDX      6
#define DM_LAST_POS_DATE_IDX 7
#define DM_TASKS_IDX         8
#define DM_ID_IDX            9
#define DM_PASSWD_IDX        10

class QTimer;

class DeviceModel : public QAbstractItemModel
{
    Q_OBJECT
public:
    explicit DeviceModel(QObject *parent = 0);

    int           rowCount   (const QModelIndex &parent = QModelIndex()) const;
    int           columnCount(const QModelIndex &parent = QModelIndex()) const;
    Qt::ItemFlags flags      (const QModelIndex &index)                  const;
    QVariant      data       (const QModelIndex &index, int role)        const;
    QVariant      headerData (int section, Qt::Orientation orientation,
                              int role = Qt::DisplayRole)                const;
    QModelIndex   index      (int row, int column,
                              const QModelIndex &parent = QModelIndex()) const;
    QModelIndex   parent     (const QModelIndex &index)                  const;


    std::vector<MoleLine> moles        () const;
    void                  setMoles     (const std::vector<MoleLine> &mol);
    void                  setIncoming  (const std::vector<IncomingLine>& incoming);
    void                  setMoleTasks (const std::vector<MoleTask>& tasks);

public slots:
    void deviceListSize(int);
    void lastInfoSize  (int);
    void moleTaskSize  (int);

private slots:
    void onUpdateTime();
private:
    QBrush      backgroundRole(int row) const;
    QStringList moleTasks     (int row) const;
private:
    std::vector<MoleLine>     moles_;
    std::vector<QColor>       row_colors_;
    std::vector<IncomingLine> last_incoming_;
    QMap<int,std::vector<MoleTask> > mole_tasks_;

    QTimer*                   update_timer_;
};

#endif // DEVICEMODEL_HPP
